<?php

class DActiveRecord extends CActiveRecord {

    protected function beforeSave() {
        parent::beforeSave();

        if ($this->getIsNewRecord()) {
            $this->setAttribute('insert_by', Yii::app()->user->login_id);
            $this->setAttribute('insert_time', date('Y-m-d H:i'));
        }

        $this->setAttribute('update_by', Yii::app()->user->login_id);
        $this->setAttribute('update_time', date('Y-m-d H:i'));

        return true;
    }

    public function getNextSequence($sName) { //for PostgreSQL
        $sSQL = "SELECT nextval('" . $sName . "');";
        return Yii::app()->db->createCommand($sSQL)->queryScalar();
    }
    
    public function getAutoIncrement($tblName) { //for MySQL
        $sql = 'SHOW TABLE STATUS WHERE `Name` = "' . $tblName . '"';
        $row = Yii::app()->db->createCommand($sql)->queryRow();

        return (int) $row['Auto_increment'];
    }

}